You want to use Hold to build up your With statement. Here is one way; there may be a simpler.
You want to use Hold to build up your With statement. Here is one way; there may be a simpler: In1:= SetAttributesWithRules, HoldRest In2:= WithRulesrules_, expr_ := With @@ AppendApplySet, Hold@rules, {2}, Unevaluatedexpr Test it out: In3:= fargs___ := Print{args} In4:= rules = {a -> b, c -> d}; In5:= WithRulesrules, fa, c During evaluation of In5:= {b,d} (I used Print so that any bug involving me accidentally evaluating expr too early would be made obvious. ).
I have been using the following form of WithRules for a long time. Compared to the one posted by Andrew Moylan, it binds sequentially so that you can say e.g. WithRules{a->b+1, b->2},expr and get a expanded to 3: SetAttributesWithRules, HoldRest WithRulesrules_, expr_ := ReleaseHold@Module{notSet}, Quiet With{args = Reverserules /. Rulea_, b_ -> notSeta, b}, FoldWith{#2}, #1 &, Hold@expr, args /.
NotSet -> Set, With::lvw This was also posted as an answer to an unrelated question, and as noted there, it has been discussed (at least) a couple of times on usenet: A version of With that binds variables sequentially Add syntax highlighting to own command HTH EDIT: Added a ReleaseHold, Hold pair to keep expr unevaluated until the rules have been applied.
Thanks Janus! Now that you mention it, I remember at least one of those "sequential With" discussions in mathgroup. Your answer doesn't quite behave the way that I wanted - see the edits.
– Simon Mar 4 '11 at 6:08 1 @Simon, fixed it to actually be an answer :) – Janus Mar 4 '11 at 8:34.
One problem with Andrew's solution is that it maps the problem back to With, and that does not accept subscripted variables. So the following generates messages. WithRules{Subscriptx, 1 -> 2, Subscriptx, 2 -> 3}, PowerSubscriptx, 1, Subscriptx, 2 Given that With performs syntactic replacement on its body, we can set WithRules alternatively as follows: ClearAllWithRules; SetAttributesWithRules, HoldRest; WithRulesr : {(_Rule | _RuleDelayed) ..}, body_ := ReleaseHoldHoldbody /.
R Then In113:= WithRules{Subscriptx, 1 -> 2, Subscriptx, 2 -> 3}, Subscriptx, 1^Subscriptx, 2 Out113= 8 Edit: Addressing valid concerns raised by Leonid, the following version would be safe: ClearAllWithRules3; SetAttributesWithRules3, HoldRest; WithRules3r : {(_Rule | _RuleDelayed) ..}, body_ := Developer`ReplaceAllUnheldUnevaluatedbody, r Then In194:= WithRules3{Subscriptx, 1 -> 2, Subscriptx, 2 -> 3}, Subscriptx, 1^Subscriptx, 2 Out194= 8 In195:= WithRules3{x -> y}, fy_ :> Functionx, x + y Out195= fy_ :> Functionx, x + y Edit 2: Even WithRules3 is not completely equivalent to Andrew's version: In206:= WithRules3{z -> 2}, fy_ :> Functionx, x + y + z Out206= fy_ :> Functionx, x + y + z In207:= WithRules{z -> 2}, fy_ :> Functionx, x + y + z Out207= fy$_ :> Functionx$, x$ + y$ + 2.
If that is all that is required, cannot one merely do UnevaluatedSubscriptx, 1^Subscriptx, 2 /. {Subscriptx, 1 -> 2, Subscriptx, 2 -> 3}? – Mr.Wizard Mar 4 '11 at 13:56 @Mr.Wizard Yes, that is an alternative.
– Sasha Mar 4 '11 at 14:18 3 There are subtle differences in semantics of With and rule-based substitutions, that will show up when the code in "body" has nested scoping constructs with local variable names colliding with some of the symbols in rules. Using With will favor inner constructs, using rules will favor outer rules and break the inner scoping constructs bindings. What is advantageous, depends on the code in question, but my feeling is that the "With" way is generally safer.
– Leonid Shifrin Mar 4 '11 at 15:50 Nice one. Having a version of With that handles non-symbols, such as subscripted variables, is a really handy generalization. – Andrew Moylan Mar 4 '11 at 21:01 1 @Andrew As I already commented, this is not really a version of With, but rather a utility automating external rule application to an unevaluated expression.
Try, for example, WithRules{x -> y}, fy_ :> Functionx, x + y with both yours and @Sasha's solutions, to see the difference. In the context of lexical scoping, I consider rule substitution far more dangerous. It is, in particular, responsible for the leaks in the functional abstraction (Function) in Mathematica, one of which the above code snippet demonstrates.
– Leonid Shifrin Mar 4 '11 at 22:05.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.